package cn.yeyiu.sexyiu.controller

import cn.yeyiu.sexyiu.service.FileService
import cn.yeyiu.sexyiu.service.MediaService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

//host/file/video/anime/fileName?ctg=2010_10
//host/file/img/other/fileName?ctg=motion
@RestController
@RequestMapping(value = ["/file"])
class MediaController {
    @Autowired
    var fs: FileService? = null

    @Autowired
    var ms: MediaService? = null

    @GetMapping("/video/{sexyiuType}/{detailType}/{fileName}")
    fun getVideo(
        request: HttpServletRequest, response: HttpServletResponse,
        @PathVariable sexyiuType: String,
        @PathVariable detailType: String,
        @PathVariable fileName: String
    ) {
        if (fileName == "null") return
        ms!!.writeVideo(request, response, fs!!.getVideoFile(sexyiuType, detailType, fileName))
    }

    @GetMapping("/img/{sexyiuType}/{detailType}/{fileName}")
    fun getImg(
        response: HttpServletResponse,
        @PathVariable sexyiuType: String,
        @PathVariable detailType: String,
        @PathVariable fileName: String
    ) {
        if (fileName == "null") return
        ms!!.writeImg(response, fs!!.getImgFile(sexyiuType, detailType, fileName))
    }

    @GetMapping(value = ["/manga/{title}/logo"])
    fun getMangaLogo(
        response: HttpServletResponse,
        @PathVariable title: String
    ) {
        if (title == "null") return
        ms!!.writeImg(response, fs!!.getMangaLogo(title))
    }

    @GetMapping(value = ["/manga/{title}/{ep}/logo"])
    fun getMangaEp(
        response: HttpServletResponse,
        @PathVariable title: String,
        @PathVariable ep: String
    ) {
        if (title == "null") return
        ms!!.writeImg(response, fs!!.getMangaEpLogo(title, ep))
    }

    @GetMapping(value = ["/manga/{title}/{ep}/{idx}"])
    fun getManga(
        response: HttpServletResponse,
        @PathVariable title: String,
        @PathVariable ep: String,
        @PathVariable idx: String
    ) {
        if (title == "null") return
        ms!!.writeImg(response, fs!!.getMangaFile(title, ep, idx))
    }
}